<?php
/**
 * 门店卡券管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;


use Model\ActivityCouponMod;
use Model\StoreCouponMod;

class StoreCoupon extends Action
{
    private $StoreCoupon = null;   //门店卡券
    private $ActivityCoupon = null; //活动投放的卡券

    public function __construct()
    {
        parent::__construct();
        $this->StoreCoupon = new StoreCouponMod();
    }

    /**
     * 创建门店卡券
     * @param data 新增数据字段
     *      data.uniacid 公众号id
     *      data.store_id 门店id
     *      data.name 卡券名字
     *      data.type 卡券名字
     *      data.cost 卡券相关信息 面值 等等其他卡券的相关信息
     *      data.rule 卡券规则说明
     * @return coupon_id  返回新增的卡券id
     */
    public function add()
    {
        $this->_gpc['data']['edit_time'] = TIMESTAMP;
        $this->_gpc['data']['images'] = json_encode($this->_gpc['data']['images']);
        if($_result = $this->StoreCoupon->insert($this->_gpc['data'])){
            $this->_result = array('status'=>'success','data'=>pdo_insertid());
        }
        $this->result($this->_result);
    }

    /**
     * 编辑卡券
     * @param data  修改的数据字段
     *      data.name 卡券名字
     *      data.type 卡券类型
     *      data.cost 卡券相关信息 面值 等等其他卡券的相关信息
     *      data.rule 卡券规则说明
     */
    public function edit()
    {
        $this->ActivityCoupon = new ActivityCouponMod();
        $this->ActivityCoupon->update(array('status'=>2),array('coupon_id'=>$this->_gpc['coupon_id']));
        $this->_gpc['data']['images'] = json_encode($this->_gpc['data']['images']);
        if($_result = $this->StoreCoupon->update($this->_gpc['data'],array('id'=>$this->_gpc['coupon_id']))){
            $this->StoreCoupon->update(array('edit_time'=>TIMESTAMP),array('id'=>$this->_gpc['coupon_id']));
            $this->_result = array('status'=>'success','data'=>pdo_insertid());
        }
        $this->result($this->_result);
    }

    /**
     * 获取列表
     * @param store_id 门店id
     * @param limit [0,10]请求下标数量
     * @param type 卡券类型 qb
     */
    public function getList()
    {
        $_where = array('store_id'=>$this->_gpc['store_id'],'status'=>1);
        if($this->_gpc['type'] != 'qb'){
            $_where['type'] = $this->_gpc['type'];
        }
        $_list = $this->StoreCoupon->findAll($_where,array('order'=>array('id','desc'),'limit'=>$this->_gpc['limit']),array('id','name','type','cost','images'));
        if(count($_list)){
            if($this->_gpc['select'] == 1) $this->ActivityCoupon = new ActivityCouponMod();
            foreach($_list as $_k=>$_v){
                $_list[$_k]['cost'] = json_decode(htmlspecialchars_decode($_v['cost']),true);
                $_images = json_decode($_v['images'],true);
                if(count($_images) > 0){
                    $_list[$_k]['cover'] = tomedia($_images[0]);
                }
                //投放中的卡券数量
                if($this->_gpc['select'] == 0){
                    $_list[$_k]['put_number'] = $this->StoreCoupon->putNumber($_v['id'],'number') - $this->StoreCoupon->putNumber($_v['id'],'give_number');
                }else{
                    //请求投放信息
                    if($_activity_coupon = $this->ActivityCoupon->findOne(array('activity_id'=>$this->_gpc['activity_id'],'coupon_id'=>$_v['id']),array('id','status','goal','number','give_number','use_limit_type','limit_time'))){
                        $_activity_coupon['put_id'] = $_activity_coupon['id'];
                        unset($_activity_coupon['id']);
                        $_list[$_k] = array_merge($_list[$_k],$_activity_coupon);
                        if($_activity_coupon['use_limit_type'] == 2) $_list[$_k]['time'] = date('Y-m-d', $_activity_coupon['limit_time']);
                        unset($_activity_coupon);
                    }else{
                        $_list[$_k]['status'] = 0;
                    }
                }
                unset($_list[$_k]['images']);
            }
            unset($_images);
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 通过卡券id获取卡券信息
     * @param coupon_id 卡券id
     */
    public function find()
    {
        $_one = $this->StoreCoupon->findOne(array('id'=>$this->_gpc['coupon_id']),array('name','images','type','cost','rule'));
        if($_one){
            $_cost = json_decode(htmlspecialchars_decode($_one['cost']),true);
            $_one['face_value'] = $_cost['face_value'];
            if(isset($_cost['minus_value'])) $_one['minus_value'] = $_cost['minus_value'];
            $_images = json_decode($_one['images'],true);
            if(count($_images) > 0){
            $_temp_images = array();
                foreach($_images as $_v){
                    $_temp_images[] = array(
                        'url'=>tomedia($_v),
                        'path'=>$_v
                    );
                }
                $_one['images'] = $_temp_images;
            }else{
                $_one['images'] = array();
            }
            unset($_one['cost'],$_temp_images);
            $this->_result = array('status'=>'success','data'=>$_one);
        }
        $this->result($this->_result);
    }

    /**
     * 通过卡券id删除指定的卡券
     * @param coupon_id 卡券id
     */
    public function delete()
    {
        if($this->StoreCoupon->update(array('status'=>9),array('id'=>$this->_gpc['coupon_id']))){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }
}